﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using UniMana.Utils.Data;
using UniMana.Data;

namespace UniMana.Win.Training
{
    public partial class FrmPersonnelRegistSession_Detail : DevExpress.XtraEditors.XtraForm
    {
        private DataTable m_tblTable;
        private DBMain m_data = new DBMain();
        private string m_strPersonKey = "";
        private string m_strPersonName;
        DateTime m_dStart = new DateTime();
        DateTime m_dEnd = new DateTime();

        public FrmPersonnelRegistSession_Detail(DateTime m_dStart, DateTime m_dEnd, string m_strPersonKey, string m_strPersonName)
        {
            InitializeComponent();
            this.m_dStart = m_dStart;
            this.m_dEnd = m_dEnd;
            this.m_strPersonKey = m_strPersonKey;
            this.m_strPersonName = m_strPersonName;
        }

        private void FrmPersonnelRegistSession_Detail_Load(object sender, EventArgs e)
        {
            DisplayDatetime();
            LoadDataOfPerson();
            grcInfor.Text = "Lịch làm việc của " + m_strPersonName;
            rluTraining_WeekDay.GetData(m_data.m_DCReadOnly.Training_WeekDays, "Training_WeekDayKey", "Name");
            rluTraining_Session.GetData(m_data.m_DCReadOnly.Training_Sessions, "Training_SessionKey", "Name");
            rluPersonnel_WorkType.GetData(m_data.m_DCReadOnly.Personnel_WorkTypes, "Personnel_WorkTypeKey", "Name");
        }

        private void LoadDataOfPerson()
        {
            m_tblTable = (this.m_data.m_DCReadOnly.Personnel_Personnel_Sessions.GetDataOfPerson(m_strPersonKey, m_dStart, m_dEnd)).ToDataTable();
            if (m_tblTable.Rows.Count != 0)
            {
                gctView.DataSource = m_tblTable;
            }
            else
            {
                gctView.DataSource = null;
            }
        }

        private void DisplayDatetime()
        {
            if (m_dStart.ToString() == "1/1/0001 12:00:00 PM")
            {
                dteStart.EditValue = null;                
            }
            else
            {
                dteStart.EditValue = m_dStart;
            }
            if (m_dEnd.ToString() == "1/1/0001 12:00:00 PM")
            {
                dteEnd.EditValue = null;                
            }
            else
            {
                dteEnd.EditValue = m_dEnd;
            }
        }

        private void dteStart_EditValueChanged(object sender, EventArgs e)
        {
            m_dStart = (DateTime)dteStart.EditValue;
            LoadDataOfPerson();
        }

        private void dteEnd_EditValueChanged(object sender, EventArgs e)
        {
            m_dEnd = (DateTime)dteEnd.EditValue;
            LoadDataOfPerson();
        }
    }
}
